'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
main
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	dim root:root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	Dim doc
	Set doc = application.ActiveDocument
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	Dim pcbVias,pcbVia,pcbViaName,pcbViaX,pcbViaY,pcbViaNet,viaSpan,viaSpanStart,viaSpanEnd
	Dim ViaPadstack
	Dim pcbPins,pcbPin,rNET,rvia

	set pcbPins=doc.Pins(epcbSelectSelected)
	for each pcbPin In pcbPins

		set rNET=pcbPin.net
		'msgbox rnet
		
	next
	
	set pcbVias=doc.Vias(epcbSelectSelected)
	for each pcbVia In pcbVias
	
        pcbViaName = pcbVia.Name
        
        viaSpanStart = pcbVia.StartLayer
        viaSpanEnd = pcbVia.EndLayer
        pcbViaX = pcbVia.PositionX
        pcbViaY = pcbVia.PositionY
        pcbViaNet = pcbVia.Net.Name

		If pcbVia.Selected = True Then
		
        Set ViaPadstack = doc.PutPadstack(viaSpanStart, viaSpanEnd, pcbViaName, False, False)        
        pcbVia.delete
        doc.TransactionStart (0)
        set rvia= doc.PutVia(pcbViaX, pcbViaY, ViaPadstack, rNET, Nothing, epcbUnitCurrent, epcbAnchorNone)
        doc.TransactionEnd (True)  
        end if 
	next

	Dim pcbTraces,pcbTrace,PasteTraceObj
	Dim TraceALyr,TraceApntsArr,TraceAnumPoints,TraceAWidth
	set pcbTraces=doc.Traces(epcbSelectSelected)
	for each pcbTrace In pcbTraces
		TraceApntsArr = pcbTrace.Geometry.pointsarray
        TraceAnumPoints = UBound(pcbTrace.Geometry.pointsarray, 2) + 1
        TraceALyr = pcbTrace.Layer
        TraceAWidth = pcbTrace.Geometry.LineWidth
        pcbTrace.delete()
        doc.TransactionStart (0)
        Set PasteTraceObj = doc.PutTrace(TraceALyr, rNET, TraceAWidth, TraceAnumPoints, TraceApntsArr, Nothing, epcbAnchorNone, epcbUnitCurrent)
        doc.TransactionEnd (True)
    next
end sub

